/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "hot_data_lower_limit_fuzzer.h"

const char hcHotDataLowerLimit[] = "/sys/fs/f2fs/loop1/hc_hot_data_lower_limit";

namespace OHOS {

/*static bool OpenCloseHcEnable(uint32_t data, size_t size)
{
    char buffer[size];
    int fd = open(devHcEnable, RDWR);
    if (fd < 0) {
        printf("open %s failed\n", devHcEnable);
        return false;
    }
    int ret = read(fd, buffer, size);
    if(ret > 0){
        printf("%s read success\n", devHcEnable);
    }
    if(ret < 0){
        printf("%s read fail, data = %d\n", devHcEnable, data);
        return false;
    }
    if(ret == 0){
        printf("%s read finish\n", devHcEnable);
    }
    ret = write(fd, (void *)data, size);
    if(ret > 0){
        printf("%s write success\n", devHcEnable);
    }
    if(ret < 0){
        printf("%s write fail\n", devHcEnable);
        return false;
    }
    return true;
}
*/
bool HcHotDataLowerLimitFuzzer(const uint8_t* data, size_t size)
{
    // uint32_t dataOpenClose = 1;
    // OpenCloseHcEnable(dataOpenClose, sizeof(uint32_t));
    char buffer[size];
    int fd = open(hcHotDataLowerLimit, RDWR);
    if (fd < 0) {
        printf("open %s failed\n", hcHotDataLowerLimit);
        return false;
    }
    int ret = read(fd, buffer, size);
    if(ret > 0){
        printf("%s read success\n", hcHotDataLowerLimit);
    }
    if(ret < 0){
        printf("%s read fail, data = %s\n", hcHotDataLowerLimit, data);
        return false;
    }
    if(ret == 0){
        printf("%s read finish\n", hcHotDataLowerLimit);
    }
    ret = write(fd, data, size);
    if(ret == 0){
        printf("%s write success\n", hcHotDataLowerLimit);
    }
    close(fd);
    return true;
}

} // namespace OHOS

/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
    /* Run your code on data  hc_hot_data_lower_limit*/
    OHOS::HcHotDataLowerLimitFuzzer(data, size);
    return 0;
}

